<?php
namespace services\models;

/**
 * 商品扩展信息
 *
 * @author huipeng.teng@liketry.com
 */
class GoodsExt extends \common\models\GoodsExt
{
    public static function getDb() {
        return \Yii::$app->db;
    }

    public static function getRedis() {
        return \Yii::$app->redis->goods;
    }

    /**
     * 通过商品ID获取商品扩展信息
     * @param int $goods_id 商品ID
     * @param string $fields 要查询的字段
     * @return array 商品信息
     */
    public static function findByGoodsId($goods_id, $fields = '*') {
        $var = self::find()->select($fields)->where('goods_id=:goods_id', [':goods_id' => $goods_id])->one();
        if(!$var) {
            return [];
        }
        return \yii\helpers\ArrayHelper::toArray($var);
    }

    /**
     * 通过商品ID获取商品详情信息
     * @param int $goods_id 商品ID
     * @return string 商品详情信息
     */
    public static function getGoodsMain($goods_id) {
        $goods_main = self::getRedis()->get('goods_main_' . $goods_id);
        if($goods_main !== NULL) {
            return $goods_main;
        }

        $var = self::find()->select('goods_main')->where('goods_id=:goods_id', [':goods_id' => $goods_id])->one();
        if(!$var) {
            return [];
        }
        
        $array = \yii\helpers\ArrayHelper::toArray($var);
        if(!empty($array)) {
            self::getRedis()->set('goods_main_' . $goods_id, $array['goods_main']);
        }

        return $array['goods_main'];
    }
}
